﻿using smp.RabbitMq.Configuration;
using smp.RabbitMq.Strategies;

namespace smp.RabbitMq
{
    public class RabbitMqFactory : IRabbitMqFactory
    {
        private readonly ILog _log;
        private readonly RabbitMqSection _rabbitMqSection;

        public RabbitMqFactory()
            : this(new DebugLog())
        {
        }

        public RabbitMqFactory(ILog log)
        {
            _log = log;
            InstancesFactory.Log = _log;
            _rabbitMqSection = new RabbitMqSection();
        }

        #region Implementation of IRabbitMqFactory

        public IPublisher CreatePublisher(string configurationKey)
        {
            return CreatePublisher(_rabbitMqSection.GetPublisher(configurationKey));
        }

        public IPublisher CreatePublisher(IRabbitMqConfiguration configuration)
        {
            return new Publisher(configuration);
        }


        public IRequester CreateRequester(string configurationKey)
        {
            return CreateRequester(_rabbitMqSection.GetRequester(configurationKey));
        }

        public IRequester CreateRequester(IRabbitMqConfiguration configuration)
        {
            return new Requester(configuration);
        }


        public ISubscriber CreateSubscriber(SubscriberHandlers handlers)
        {
            return CreateSubscriber(handlers, new BasicSubscriberStrategy(), _rabbitMqSection.GetSubscriber());
        }

        public ISubscriber CreateSubscriber(SubscriberHandlers handlers, ISubscriberStrategy strategy)
        {
            return CreateSubscriber(handlers, strategy, _rabbitMqSection.GetSubscriber());
        }

        public ISubscriber CreateSubscriber(SubscriberHandlers handlers, IRabbitMqConfiguration configuration)
        {
            return CreateSubscriber(handlers, new BasicSubscriberStrategy(), configuration);
        }

        public ISubscriber CreateSubscriber(SubscriberHandlers handlers, ISubscriberStrategy strategy, IRabbitMqConfiguration configuration)
        {
            InstancesFactory.SubscriberConfiguration = configuration;
            return new Subscriber(configuration, handlers, strategy);
        }


        public IResponderSubscriber CreateResponderSubscriber(ResponderHandlers handlers)
        {
            return CreateResponderSubscriber(handlers, new BasicResponderStrategy(), _rabbitMqSection.GetSubscriber());
        }

        public IResponderSubscriber CreateResponderSubscriber(ResponderHandlers handlers, IResponderStrategy strategy)
        {
            return CreateResponderSubscriber(handlers, strategy, _rabbitMqSection.GetSubscriber());
        }

        public IResponderSubscriber CreateResponderSubscriber(ResponderHandlers handlers, IRabbitMqConfiguration configuration)
        {
            return CreateResponderSubscriber(handlers, new BasicResponderStrategy(), configuration);
        }

        public IResponderSubscriber CreateResponderSubscriber(ResponderHandlers handlers, IResponderStrategy strategy, IRabbitMqConfiguration configuration)
        {
            InstancesFactory.SubscriberConfiguration = configuration;
            return new ResponderSubscriber(configuration, handlers, strategy);
        }

        #endregion
    }
}
